/**
* @Title: indicatorsAdd_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-XIEQI001
* @date 2014年10月14日 下午2:38:47
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('IndicatorAddCtrl', ['$scope','$http','$state','$modalInstance','BaseURL','Modal',
	                                function($scope,$http,$state,$modalInstance,baseUrl,Modal){
		
		$scope.indicatorModalColumns = [
							{label:'序号',map:'',width:'10%',cellTemplate:'<span ng-bind="$index + 1"></span>'},
							{label:'指标编码',map:'kpiCode',width:'40%'},
							{label:'指标名称',map:'kpiName',width:'50%'}
						];
		var currentPage = 0;
		
		$scope.form = {};
		//查询
		$scope.onSubmit = function(){
			currentPage = 1;
			query();
		}
		//重置
		$scope.onReset = function(){
			$scope.form = {};
		}
		$scope.indicatorModalPage = {};
		
		
		//每页显示数
		$scope.indicatorModalPage.limit = 10;
		
		var query =	function(){
			$http({
				method : 'GET',
				cache : false,
				url : baseUrl + 'indicatorConfig/getIndicatorAddData.do',
				params : {
					kpiCode:$scope.form.indicatorCode,
					kpiName:$scope.form.indicatorName,
					currentPage : currentPage,
					limit : $scope.indicatorModalPage.limit
				}
			}).success(function(response, status, headers, config) {
				if(currentPage == 1){
					$scope.indicatorModalPage.currentPage = currentPage;
				}
				$scope.indicatorModalData = response.root;
				$scope.indicatorModalPage.total = response.total;
			});
		}
		$scope.onSubmit();
		
		var checkedRows = [];
		
		function findRow(data) {
			if(data == null) {
				return -1;
			}
			for(var i=0;i<checkedRows.length;i++) {
				if(checkedRows[i].kpiId == data.kpiId) {
					return i;
				}
			}
			return -1;
		}
		//监控chenbox 选中事件
		$scope.$on('fwatable.checked',function(e,d){
			var index = findRow(d);
			if(index == -1 && d != null) {
				checkedRows.push(d);
			}
		});
		//监控chenbox 不选中事件
		$scope.$on('fwatable.unchecked',function(e,d){
			var index = findRow(d);
			if(index != -1) {
				checkedRows.splice(index,1);
			}
		});
		
		//确定
		$scope.ok = function(){
			var kpiIds = [];
			angular.forEach(checkedRows,function(data,index){
				kpiIds.push(data.kpiId);
			});
			$http({
				method: 'GET',
				cache:false,
			    url: baseUrl + 'indicatorConfig/addIndicator.do',
			    params:{
			    	tmp:new Date(),
			    	kpiIds:kpiIds.join(",")
			    }
			}).success(function(response){
				if(response.starts == 'Ok'){
					$modalInstance.close("Ok");
				}
				Modal.alert(response.msg);
			}).error(function(e){
				Modal.alert(e.msg);
			});
			
		}
		//关闭model
		$scope.cancel = function() {
  		  $modalInstance.dismiss();
  	    }
		//分页监听
		$scope.$on('pageTool.skipPage',function(e,v){
			$scope.indicatorModalPage.currentPage = v;
			currentPage = v;
			query();
		});
	}]);
})();